package web.department;

import bean.Department;
import bean.Type;
import com.alibaba.fastjson.JSON;
import entity.DepartmentEntity;
import entity.TypeEntity;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "DepartmentTotalList",urlPatterns = "/dao/departmentTotalList")
public class DepartmentTotalList extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        EntityManager em = (EntityManager) request.getAttribute("_entity.Manager");
        Query query = em.createQuery("select v from DepartmentEntity v");
        List<DepartmentEntity> list = query.getResultList();
        List<Department> departmentList = new ArrayList<>();
        for (int i=0; i<list.size(); i++) {
            Department department = new Department(""+list.get(i).getDid(),""+list.get(i).getName());
            departmentList.add(department);
        }
        response.getWriter().write(URLEncoder.encode(JSON.toJSONString(departmentList),StandardCharsets.UTF_8));
    }
}
